// #include <stdio.h>

// #include "shm_operation.h"

// int check()
// {
//     int i = 1;
//     return (*(char*)&i);
// }
 
 
// int main()
// {
//     // char test[40];
//     // scanf("%s\n",test);
//     // scanf("%s\n",test+20);

//     // printf("%s\n",test);
//     // printf("%s\n",test+20);

    
//     // int ret = check();   
//     // if (ret == 1)     printf("小端存储");
//     // else     printf("大端存储");
//     // return 0;
    
//     int shmid=create_shm(1);
//     char* shm=connect_shm(shmid);

//     printf("%s",&shm);

// }

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
#include "common.h"
#include "psql_operation.h"

int main(int argc, char const *argv[])
{
	//获取唯一的key
	key_t key = ftok("/", 2000);
	// if(key<0) 	printf("%d ",key);
	//获取共享内存的标识(分配物理内存)
	int shm_id = shmget(key, 4095, IPC_CREAT|0666);
	// printf("%d ",shm_id);
	//将虚拟内存和物理内存建立映射
	char *buf = (char *)shmat(shm_id, NULL, 0);

	//操作虚拟内存
	// strcpy(buf, "hello2 shm");
	// printf("%s\n",buf);
	struct Connect_Info connect_info;
	struct Connect_Info* p_con_info=(struct Connect_Info*)buf;

	memcpy(&connect_info,buf,sizeof(struct Connect_Info));
    


	for(int i=0;i<2*sizeof(struct Connect_Info);i++)
	{
		printf("%p %c %c\n",(buf+i),*(buf+i),*(((char*)&connect_info)+i));
	}
	printf("\n\n1%p\n",buf);
	printf("2%p\n",&connect_info);
	printf("3%p\n",&(connect_info.username));
	printf("4%p\n",p_con_info);
	printf("4%p\n",p_con_info+1);
	printf("5%p\n",p_con_info->username);
	printf("6%p\n",&p_con_info);
	printf("7%s\n",buf+40);
	printf("8%d\n",p_con_info->connectfd);
	// printf("%s",p_con_info->connectfd);
	// printf("%s",p_con_info->client_addr);
	printf("%s",p_con_info->username);
	// printf("%d ",sizeof(struct sockaddr_in));
	// printf("%s",p_con_info->location);
	// printf("%s",p_con_info->next_connect_info);
	for(int i=0;i<5;i++)
	{
		printf("%s\n",(p_con_info+i)->username);
	}



	
	int user_id=get_id_by_username("test1");
	int user_shm_id=get_user_shm_id(1024,user_id);
	printf("%d\t",user_id);
	
	printf("%d\t",user_shm_id);
	// user_shm_id=327743;
	char* user_shm = (char *)shmat(user_shm_id, NULL, 0);
    printf("%p\t",user_shm);
	printf("\nuser_shm is %s ",user_shm);
	
	user_id=get_id_by_username("test3");
	user_shm_id=get_user_shm_id(1024,user_id);
	printf("%d\t",user_id);
	
	printf("%d\t",user_shm_id);
	// user_shm_id=327743;
	user_shm = (char *)shmat(user_shm_id, NULL, 0);
	// memcpy(user_shm,"test1234"+0,9);
    printf("%p\t",user_shm);
	printf("\nuser_shm is %s ",user_shm);
	
	//释放映射
	shmdt(buf);
	// printf("%d ",sizeof(long));
	// printf("%d ",sizeof(float));
	return 0;
}
